local qietian = fk.CreateSkill {
  name = "xiaobai__qietian",
}

Fk:loadTranslationTable {
  ["xiaobai__qietian"] = "且佃",
  [":xiaobai__qietian"] = "一名角色的结束阶段，你可将一张黑色牌当【无中生有】使用并交给其一张牌，然后若其没有“且守”，你可以失去此技能并令其获得“且守”。",
  ["#xiaobai__qietian"] = "且佃：你可将一张黑色牌作为【无中生有】使用",
  ["xiaobai__qietian_giveCard"] = "且佃：你需交给 %dest 一张牌",
  ["#xiaobai__qietian_giveSkill"] = "且佃：你可以失去“且佃”并令 %dest 获得“且守”",

  ["$xiaobai__qietian1"] = "战不可无耕之基，耕不可忘战之备。",
  ["$xiaobai__qietian2"] = "昼则耒耜如林，夜则刁斗森列，吴贼敢不屏息？"
}

qietian:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  prompt = "#qietian_invoke",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(qietian.name) and target.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local use, data = room:askToUseActiveSkill(player, {
      skill_name = "xiaobai__qietian_viewas",
      prompt = "#xiaobai__qietian",
      extra_data = {
        skillName = qietian.name
      }
    })
    if use and data then
      local card = Fk.skills["xiaobai__qietian_viewas"]:viewAs(player, data.cards)
      event:setCostData(self, card)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = event:getCostData(self)
    room:useCard({
      card = card,
      from = player,
      tos = {player},
      attachedSkillAndUser = {
        muteCard = true,
        skillName = qietian.name
      }
    })
    if player.dead or target.dead then return end

    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = qietian.name,
      prompt = "xiaobai__qietian_giveCard::"..target.id,
      cancelable = false,
    })
    room:obtainCard(target, cards, false, fk.ReasonGive)
    if player.dead or target.dead then return end
    
    if player:hasSkill(qietian.name) and not target:hasSkill("xiaobai__qieshou") 
        and room:askToSkillInvoke(player, {skill_name = qietian.name, prompt = "#xiaobai__qietian_giveSkill::"..target.id}) then
      room:handleAddLoseSkills(player, "-xiaobai__qietian")
      room:handleAddLoseSkills(target, "xiaobai__qieshou")
    end
  end,
})

local qietian_viewas = fk.CreateSkill{
  name = "xiaobai__qietian_viewas"
}

qietian_viewas:addEffect("viewas", {
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).color == Card.Black
  end,
  handly_pile = true,
  view_as = function (self, player, cards)
    if #cards == 0 then return end
    local c = Fk:cloneCard("ex_nihilo")
    c.skillName = "xiaobai__qietian"
    c:addSubcards(cards)
    return c
  end
})

local skels = {}
table.insert(skels, qietian)
table.insert(skels, qietian_viewas)

return skels
